libname bond 'D:\name your local directory';
libname factor 'D:\name your local directory\Ken French Data Library';
%Include 'D:\name your local directory\Macros.sas';


data Reg_varname_Mon_Ind; 
length Vname $50 Vname_suffix $10; 
input Vname $ Vname_suffix $;
datalines;
BetaUNC_1m_d1 _Coef
BetaUNC_1m_d6 _Coef
BetaUNC_1m_d12 _Coef
BetaUNC_Level_1m _Coef
; run;


data Reg_varname_Mon_Ind_Chars; 
length Vname $100 Vname_suffix $20; 
input Vname $ Vname_suffix $;
datalines;
BetaMKT _Coef
    ILLIQ         _Coef
    Rating      _Coef
    TMaturity 	_Coef
	AmountOut   _Coef
   ; run;


%LET IN=LTB5;


data TRACE;
set bond.&IN;
AmountOut=AmountOut/1000; *in billions;
yyyymm=year(date)*100+month(date);
if yyyymm<=202112; 
run;


** Input dataset;
data temp0;
set TRACE;
rankdate = intnx('month', date, -1, 'beginning'); 
FORMAT rankdate yymmn6.;
rename return=returndate;
drop rating TMaturity AMOUNTOUT ILLIQ price N_ILLIQ;
run;


** Add lagged variables;
proc sql;
create table temp1 as select a.*, b.rating, b.TMaturity, b.AMOUNTOUT, b.ILLIQ, b.return as lagreturn, b.price as lagprice, b.N_ILLIQ
from temp0 as a left join TRACE as b
on a.complete_cusip=b.complete_cusip and intck('month',b.date,a.date)=1;
run;

proc sort data=temp1;
by complete_cusip date;
run;


data temp2;
set temp1;
if not missing(AmountOut);
if TMaturity<=1 then delete;
run;



%GetRegNameVec(dname=Reg_varname_Mon_Ind,Varrange=(1),type=1,NVarInd=Partial);  *for factors;
%GetRegNameVec(dname=Reg_varname_Mon_Ind_Chars,Varrange=(1,2,3,4,5),type=2,NVarInd=Partial); *for characteristics;
%VWRank(dname=temp2, rankdate=rankdate, rankvar=&Vnamelist);
%singlesort_adjupNEW(data=ewretdat, factors=factor.factors, byvar=, rankvar=r_rankvar, timevar=date, lag=3, var=ewret, outr=outr, outc=outc);


data bond.BetaUNC_1m_d1;
set outcchar;
run;

%GetRegNameVec(dname=Reg_varname_Mon_Ind,Varrange=(2),type=1,NVarInd=Partial);  *for factors;
%GetRegNameVec(dname=Reg_varname_Mon_Ind_Chars,Varrange=(1,2,3,4,5),type=2,NVarInd=Partial); *for characteristics;
%VWRank(dname=temp2, rankdate=rankdate, rankvar=&Vnamelist);
%singlesort_adjupNEW(data=ewretdat, factors=factor.factors, byvar=, rankvar=r_rankvar, timevar=date, lag=3, var=ewret, outr=outr, outc=outc);


data bond.BetaUNC_1m_d6;
set outcchar;
run;

%GetRegNameVec(dname=Reg_varname_Mon_Ind,Varrange=(3),type=1,NVarInd=Partial);  *for factors;
%GetRegNameVec(dname=Reg_varname_Mon_Ind_Chars,Varrange=(1,2,3,4,5),type=2,NVarInd=Partial); *for characteristics;
%VWRank(dname=temp2, rankdate=rankdate, rankvar=&Vnamelist);
%singlesort_adjupNEW(data=ewretdat, factors=factor.factors, byvar=, rankvar=r_rankvar, timevar=date, lag=3, var=ewret, outr=outr, outc=outc);


data bond.BetaUNC_1m_d12;
set outcchar;
run;

/*Below the code generates the output for Panel A of Table 8 based on change in UNC*/

data bond.WRDS_Sample_Change;
set BetaUNC_1m_d1 BetaUNC_1m_d6 BetaUNC_1m_d12;
run;


%GetRegNameVec(dname=Reg_varname_Mon_Ind,Varrange=(4),type=1,NVarInd=Partial);  *for factors;
%GetRegNameVec(dname=Reg_varname_Mon_Ind_Chars,Varrange=(1,2,3,4,5),type=2,NVarInd=Partial); *for characteristics;
%VWRank(dname=temp2, rankdate=rankdate, rankvar=&Vnamelist);
%singlesort_adjupNEW(data=ewretdat, factors=factor.factors, byvar=, rankvar=r_rankvar, timevar=date, lag=3, var=ewret, outr=outr, outc=outc);


data bond.BetaUNC_Level_1m;
set outcchar;
run;

/*Below the code generates the output for Panel B of Table 8 based on the level of UNC*/
data bond.WRDS_Sample_level;
set BetaUNC_Level_1m;
run;
